package com.woniuxy.special.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 队列
 */
@Configuration
public class QueueConfig {

    //注册的队列
    @Bean
    public Queue registerQueue() {
        return new Queue("registerCode", true, false, false);
    }

    //登录的队列 loginCode
    @Bean
    public Queue loginQueue() {
        return new Queue("loginCode", true, false, false);
    }

    //创建订单死信队列
    @Bean
    public Queue delayOrderQueue() {
        Map<String, Object> map = new HashMap<>();
        //定义该队列中消息的生存时间 单位毫秒 生存时间最终对应业务的延迟执行时间
        map.put("x-message-ttl", 5 * 60 * 1000);
        // x-dead-letter-exchange 属性用于声明死信转发到的交换机
        map.put("x-dead-letter-exchange", "delay-ex");
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的路由键名称。
        map.put("x-dead-letter-routing-key", "order");
        return new Queue("order-delay", true, false, false, map);
    }

    @Bean
    public DirectExchange delayExchange() {
        return new DirectExchange("delay-ex", true, false);
    }

    @Bean
    public Binding delayEx2delayOrderQueue() {
        return BindingBuilder.bind(delayOrderQueue()).to(delayExchange()).with("order");
    }

    //定义消费失败会走的交换机
    @Bean
    public DirectExchange errorExchange() {
        return new DirectExchange("errorExchange", true, false);
    }

    //定义消费失败会走的队列
    @Bean
    public Queue errorQueue() {
        return new Queue("errorQueue", true, false, false);
    }

    //绑定异常队列
    @Bean
    public Binding errorBind() {
        return BindingBuilder.bind(errorQueue()).to(errorExchange()).with("error");
    }
}
